package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.QtUsesrCourse;
import com.ruoyi.system.mapper.QtCourseMapper;
import com.ruoyi.system.domain.QtCourse;
import com.ruoyi.system.service.IQtCourseService;

/**
 * 课程管理Service业务层处理
 * 
 * @author zqq
 * @date 2021-11-16
 */
@Service
public class QtCourseServiceImpl implements IQtCourseService 
{
    @Autowired
    private QtCourseMapper qtCourseMapper;

    /**
     * 查询课程管理
     * 
     * @param courseId 课程管理主键
     * @return 课程管理
     */
    @Override
    public QtCourse selectQtCourseByCourseId(String courseId)
    {
        return qtCourseMapper.selectQtCourseByCourseId(courseId);
    }

    /**
     * 查询课程管理列表
     * 
     * @param qtCourse 课程管理
     * @return 课程管理
     */
    @Override
    public List<QtCourse> selectQtCourseList(QtCourse qtCourse)
    {
        return qtCourseMapper.selectQtCourseList(qtCourse);
    }

    /**
     * 新增课程管理
     * 
     * @param qtCourse 课程管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertQtCourse(QtCourse qtCourse)
    {
        qtCourse.setCourseId(IdUtils.randomUUID());
        int rows = qtCourseMapper.insertQtCourse(qtCourse);
        //insertQtUsesrCourse(qtCourse);
        return rows;
    }

    /**
     * 修改课程管理
     * 
     * @param qtCourse 课程管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateQtCourse(QtCourse qtCourse)
    {
        qtCourseMapper.deleteQtUsesrCourseByCourseId(qtCourse.getCourseId());
        //insertQtUsesrCourse(qtCourse);
        return qtCourseMapper.updateQtCourse(qtCourse);
    }

    /**
     * 批量删除课程管理
     * 
     * @param courseIds 需要删除的课程管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteQtCourseByCourseIds(String[] courseIds)
    {
        qtCourseMapper.deleteQtUsesrCourseByCourseIds(courseIds);
        return qtCourseMapper.deleteQtCourseByCourseIds(courseIds);
    }

    /**
     * 删除课程管理信息
     * 
     * @param courseId 课程管理主键
     * @return 结果
     */
    @Override
    public int deleteQtCourseByCourseId(String courseId)
    {
        qtCourseMapper.deleteQtUsesrCourseByCourseId(courseId);
        return qtCourseMapper.deleteQtCourseByCourseId(courseId);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param qtCourse 课程管理对象
     */
    public void insertQtUsesrCourse(QtCourse qtCourse)
    {
        List<QtUsesrCourse> qtUsesrCourseList = qtCourse.getQtUsesrCourseList();
        String courseId = qtCourse.getCourseId();
        if (StringUtils.isNotNull(qtUsesrCourseList))
        {
            List<QtUsesrCourse> list = new ArrayList<QtUsesrCourse>();
            for (QtUsesrCourse qtUsesrCourse : qtUsesrCourseList)
            {
                qtUsesrCourse.setCourseId(courseId);
                list.add(qtUsesrCourse);
            }
            if (list.size() > 0)
            {
                qtCourseMapper.batchQtUsesrCourse(list);
            }
        }
    }
}
